package alipay

import (
	"context"
	"encoding/json"
	"fmt"

	"gitee.com/wosylf/ltool"
)

func (a *Client) F小程序提交审核(ctx context.Context, bm ltool.BodyMap) (aliRsp *AuditApplyResponse, err error) {
	err = bm.CheckEmptyError("version_desc", "version_desc", "first_screen_shot", "second_screen_shot", "test_file_name")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = a.F新的文件上传(ctx, bm, "alipay.open.mini.version.audit.apply", 1); err != nil {
		return nil, err
	}

	aliRsp = new(AuditApplyResponse)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}

func (a *Client) F小程序查询版本构建状态(ctx context.Context, bm ltool.BodyMap) (aliRsp *BuildQueryResponse, err error) {
	err = bm.CheckEmptyError("app_version")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = a.F新的文件上传(ctx, bm, "alipay.open.mini.version.build.query"); err != nil {
		return nil, err
	}
	aliRsp = new(BuildQueryResponse)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}

func (a *Client) F小程序版本回滚(ctx context.Context, bm ltool.BodyMap) (bs []byte, err error) {
	bs, err = a.F新的文件上传(ctx, bm, "alipay.open.mini.version.rollback")
	return
}

func (a *Client) F小程序版本详情查询(ctx context.Context, bm ltool.BodyMap) (bs []byte, err error) {
	bs, err = a.F新的文件上传(ctx, bm, "alipay.open.mini.version.detail.query")
	return
}

// "2021003169650353"
func (a *Client) F查询使用模板的小程序列表(ctx context.Context, bm ltool.BodyMap) (bs []byte, err error) {
	bs, err = a.F新的文件上传(ctx, bm, "alipay.open.mini.version.list.query")
	return
}

func (a *Client) F小程序基于模板上传版本(ctx context.Context, bm ltool.BodyMap) (aliRsp *VersionUploadResponse, err error) {
	err = bm.CheckEmptyError("template_id", "app_version")
	if err != nil {
		return nil, err
	}
	var bs []byte
	bs, err = a.F新的文件上传(ctx, bm, "alipay.open.mini.version.upload")
	if err != nil {
		return
	}
	aliRsp = new(VersionUploadResponse)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}

func (a *Client) F小程序版本列表查询(ctx context.Context, bm ltool.BodyMap) (aliRsp *VersionListQueryResponse, err error) {
	var bs []byte
	bs, err = a.F新的文件上传(ctx, bm, "alipay.open.mini.version.list.query")
	if err != nil {
		return
	}
	//plug.F打印并且发送消息("string:", string(bs))
	aliRsp = new(VersionListQueryResponse)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
	aliRsp.SignData = signData
	return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
}
